<?php

	namespace Home\Controller;
	use Common\Controller\HomecommController;
	header("Content-type: text/html; charset=utf-8"); 
	/**
	 * ThinkPHP Hprose控制器类
	 */
	class CenterController extends HomecommController {
		public $uid = 0;
		function _initialize() {
			parent::_initialize();
			// if(!session('user_id')){
			// 	header('Location:'.U('Home/Logo/loin'));
			// }
			// session('user_id',null);
			// session('username',null);
			$this->uid = session('user_id');
		}

		public function index()
		{
			$memberModel = D('Member');
			// echo $memberModel->memberMd5('123456');exit;
			$user = $memberModel->where(array('id'=>$this->uid))->find();
			$this->display();
		}

		/**
		 * 修改密码
		 */
		public function modifypsw()
		{	
			if($_POST){
				$memberModel = D('Member');
				$oldPwd = $memberModel->memberMd5(I('post.old_password'));//旧密码
				$newPwd = I('post.new_password');//新密码
				$rePwd = I('post.re_password');//确认密码
				$uid = session('user_id');//会员id
				//查询旧密码是否正确
				$user = $memberModel->where(array('id'=>$uid,'password'=>$oldPwd))->find();
				if(!$user){//不存在 则密码错误
					echo json_encode(array('err'=>1,'msg'=>'旧密码错误'));exit;
				}
				if($newPwd != $rePwd){
					echo json_encode(array('err'=>1,'msg'=>'确认密码错误'));exit;
				}
				$newPwd = $memberModel->memberMd5($newPwd);
				$res = $memberModel->where(array('id'=>$uid))->save(array('password'=>$newPwd));
				if(!$res){
					echo json_encode(array('err'=>1,'msg'=>'修改密码失败'));exit;
				}else{
					header('Location:'.U('Home/Logo/loin'));
				}
			}else{
				$this->display();
			}
		}
		/**
		 * 修改手机号
		 */
		public function modifypnone()
		{
			if($_POST){
				$oldPhone = I('post.old_phone');//旧手机号
				$newPhone = I('post.new_phone');//新手机号
				$code = I('post.code');//验证码
				$memberModel = D('Member');
				$uid = session('user_id');
				$member = $memberModel->where(array('id'=>$uid,'username'=>$oldPhone))->find();
				if(!$member){//手机号不存在
					echo json_encode(array('err'=>1,'msg'=>'手机号不存在'));exit;
				}
				if($newPhone == $oldPhone){
					echo json_encode(array('err'=>1,'msg'=>'新旧手机号一致'));exit;
				}
				if(!$this->checkVerify($code)){//验证 验证码
					echo json_encode(array('err'=>1,'msg'=>'验证码错误'));exit;
				}
				//修改手机号
				$res = $memberModel->where(array('id'=>$uid))->save(array('username'=>$oldPhone,'tel'=>$oldPhone));
				if($res){
					header('Location:'.U('Home/Center/index'));
				}else{
					echo json_encode(array('err'=>1,'msg'=>'修改失败'));exit;
				}
			}else{
				$this->display();
			}
		}

		/**
		 * 消息通知
		 */
		public function msgcenter()
		{
			$SyslogModel = M('Syslog');
			$uid = session('user_id');
			$where= ' send_type = 1 or (send_type = 2 and member_allow like "%'.$uid.',%")';
			$list = $SyslogModel->where($where)->select();
			foreach ($list as $key => $val) {
				$read = explode(',',$val['memberid_str']);
				if(in_array($uid,$read)){//已读
					$list[$key]['read'] = 1;
				}else{//未读
					$list[$key]['read'] = 0;
				}
			}
			$this->assign('list',$list);
			$this->display();
		}
		/**
		 * 查看消息
		 */
		public function msgcenter_detail()
		{
			$id = I('post.id');
			$uid = session('user_id');
			$where= ' id = '.$id.' and (send_type = 1 or (send_type = 2 and member_allow like "%'.$uid.',%"))';
			$SyslogModel = M('Syslog');
			$sys = 	$SyslogModel->where($where)->find();
			if(!$sys){
				$this->error('数据错误');
			}		
			//修改消息状态
			$updateData['memberid_str'] = $sys['memberid_str'].$uid.',';
			$SyslogModel->where(array('id'=>$id))->save($updateData);
			$this->display();
		}
		/**
		 * 收藏
		 */
		public function carcart()
		{
			$collectModel = M('Collect');
			$list = $collectModel->alias('cl')
					->field("cl.id as collect_id,c.id,c.carprice,ifnull(GROUP_CONCAT(m.id ORDER BY m.orderno ),'') as imgs,trim(concat_ws(' ',if(locate(c.carbrand,c.carseries),'',c.carbrand),c.carseries,concat(c.caryear,'款'),c.caremissions,c.carkind)) as na")
					->join('left join `nc_cars` as c on cl.Carid = c.id')
					->join('left join nc_carimage m on c.id=m.carid and m.orderno < 6')
					->where(array('Cusid'=>session('user_id')))->select();
			$this->assign('list',$list);
			if(!$list[0]['collect_id']){
				$list = array();
			}
			// echo "<pre>";print_r($list);exit;
			$this->display();
		}

		/**
		 * 订单列表
		 */
		public function orderlist()
		{
			$page = I('post.page',1);
			$limit = 10;
			$start = ($page - 1) * $limit;
			$orderModel = M('Order');
			$where['member_id'] = session('user_id');
			$where['member_delete'] = 0;
			$list = $orderModel->where($where)->order('create_time DESC')->limit($start,$limit)->select();
		}
		public function orderdetail()
		{
			$this->display();
		}
	}